package rin.android.database;



import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper{
	
	private static final String NAME_DB = "cobac.db";
	private static final int VERSION = 1;
    private SQLiteDatabase mSQLiteDatabase = null;
    
    public final String TABLE_PLAYER = "TABLE_PLAYER";
    public final String ID = "ID";
    public final String NAME = "NAME"; //Tên người chơi
    public final String DOLLAR = "DOLLAR";// Số tiền mà người đó dành được
    public final String PASS = "PASS";//Pass của người chơi
    
	public Database(Context context) {
		super(context, NAME_DB, null, VERSION);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE " + TABLE_PLAYER
                + " ( "
                + ID + " INTEGER PRIMARY KEY AUTOINCREMENT" + ","
                + NAME + " TEXT NOT NULL" + ","
                + DOLLAR + " INTEGER NOT NULL" + ","
                + PASS + " INTEGER NOT NULL "
                + ");");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + TABLE_PLAYER);
        onCreate(db);
	}
	
	public void openDatabase() {
        mSQLiteDatabase = this.getWritableDatabase();
    }
	
	public void closeDatabase() {
        this.close();
    }
	
	public void execSQL(String sql) {
        this.execSQL(sql);
    }
	
	public Cursor getCursorQuery(String table, String[] columns,
            String selection, String[] selectionArgs, String groupBy,
            String having, String orderBy) {
        return mSQLiteDatabase.query(table, columns, selection, selectionArgs,
                groupBy, having, orderBy);
    }
	
	public void addPlayer(ClassPlayer mPlayer){
		//mSQLiteDatabase
		ContentValues cv = new ContentValues();
		cv.put(NAME, mPlayer.getName());
		cv.put(DOLLAR, mPlayer.getDollar());
		cv.put(PASS, mPlayer.getPass());
		mSQLiteDatabase.insert(TABLE_PLAYER, null, cv);

    }
	
	public void updatePlayer(ClassPlayer mPlayer) {
		// TODO Auto-generated method stub
		if(mSQLiteDatabase.isOpen()){ 
            ContentValues cv = new ContentValues();
            cv.put(NAME, mPlayer.getName());
            cv.put(DOLLAR, mPlayer.getDollar());
            cv.put(PASS, mPlayer.getPass());
            mSQLiteDatabase.update(TABLE_PLAYER, cv, NAME + " = '" + mPlayer.getName() + "'", null);
        }
	}
	public ArrayList<ClassPlayer> getListPlayer(){
        ArrayList<ClassPlayer> listData = new ArrayList<ClassPlayer>();
        if(mSQLiteDatabase.isOpen()){
            Cursor mCursor = getCursorQuery(TABLE_PLAYER, null, null, null, null, null, DOLLAR + " DESC");
            while(mCursor.moveToNext()){
                int dollar = mCursor.getInt(mCursor.getColumnIndex(DOLLAR));
                String name = mCursor.getString(mCursor.getColumnIndex(NAME));
                String pass = mCursor.getString(mCursor.getColumnIndex(PASS));             
                listData.add(new ClassPlayer(name, dollar, pass));
            }
            mCursor.close();
        }
        return listData;
    }

	public int isPlayer(String name, String pass){
		if(mSQLiteDatabase.isOpen()){
			Cursor mCursor = getCursorQuery(TABLE_PLAYER, null,  NAME +  " = '" + name + "' AND " + PASS + " = '" +pass + "'", null, null, null, null);
			if(mCursor.moveToFirst()){ 
				mCursor.close();
				return 1;
			}
			mCursor.close();
		}
		return 0;
	}
	public int isAvailable(String name){
		if(mSQLiteDatabase.isOpen()){
		Cursor mCursor = getCursorQuery(TABLE_PLAYER, null,  NAME +  " = '" + name + "'", null, null, null, null);
		if(mCursor.moveToFirst()){ 
			mCursor.close();
			return 0;
		}
        mCursor.close();
		}
		return 1;
	}
	public ClassPlayer getPlayerByName(String name){
		ClassPlayer mPlayer = null;
		if(mSQLiteDatabase.isOpen()){
			Cursor mCursor = getCursorQuery(TABLE_PLAYER, null,  NAME + " = '" + name +"'", null, null, null, null);
            while(mCursor.moveToNext()){
                int dollar = mCursor.getInt(mCursor.getColumnIndex(DOLLAR));
                String userName = mCursor.getString(mCursor.getColumnIndex(NAME));
                String pass = mCursor.getString(mCursor.getColumnIndex(PASS)); 
                mPlayer = new ClassPlayer(userName, dollar, pass);
            }
            mCursor.close();
		}
		return mPlayer;
	}
}
